package com.cineplanet.dew.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.cineplanet.dew.exception.DAOExcepcion;
import com.cineplanet.dew.modelo.Cine;
import com.cineplanet.dew.service.CineService;

public class CineController extends MultiActionController {

	CineService cineService;
	
	public CineService getCineService() {
		return cineService;
	}

	public void setCineService(CineService cineService) {
		this.cineService = cineService;
	}
	
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response)  {
		
		try {
			List<Cine> cines = cineService.listar();
			
			request.setAttribute("cines", cines);
		} catch (DAOExcepcion e) {
			e.printStackTrace();
		}
		
		return new ModelAndView("insertarCine");
	}
	
	public ModelAndView insertar(HttpServletRequest request,HttpServletResponse response)  {
		String nombre = request.getParameter("nombre");
		String direccion = request.getParameter("direccion");
		String numeroSalas = request.getParameter("numSalas");
		
		Cine cine = new Cine();
		cine.setCantSalas(Integer.parseInt(numeroSalas));
		cine.setDireccion(direccion);
		cine.setNombre(nombre);
		
		
		try {
			cine = cineService.insertar(cine);
			List<Cine> cines = cineService.listar();
			
			request.setAttribute("cines", cines);
		} catch (DAOExcepcion e) {
			e.printStackTrace();
		}
		
		return new ModelAndView("insertarCine");
	}
	
	public ModelAndView modificar(HttpServletRequest request,HttpServletResponse response)  {
	
		String nombre = request.getParameter("nombre");
		String direccion = request.getParameter("direccion");
		String numeroSalas = request.getParameter("numSalas");
		int idCine = Integer.parseInt(request.getParameter("idCine"));
		
		Cine cine = new Cine();
		cine.setIdCine(idCine);
		cine.setCantSalas(Integer.parseInt(numeroSalas));
		cine.setDireccion(direccion);
		cine.setNombre(nombre);
	
		try {
			cine = cineService.modificar(cine);
			List<Cine> cines = cineService.listar();
			
			request.setAttribute("cines", cines);
		} catch (DAOExcepcion e) {
			e.printStackTrace();
		}
	
		return new ModelAndView("insertarCine");
	}
	
	public ModelAndView mostrar(HttpServletRequest request,HttpServletResponse response)  {
		
		int idCine = Integer.parseInt(request.getParameter("idCine"));


		try {
			Cine cine  = cineService.obtener(idCine);
			List<Cine> cines = cineService.listar();
			
			request.setAttribute("cines", cines);
			request.setAttribute("cine", cine);
		} catch (DAOExcepcion e) {
			e.printStackTrace();
		}
	
		return new ModelAndView("modificarCine");
	}
	
	public ModelAndView eliminar(HttpServletRequest request,HttpServletResponse response)  {
		
		int idCine = Integer.parseInt(request.getParameter("idCine"));
		
		Cine cine = new Cine();
		cine.setIdCine(idCine);

		try {
			cine = cineService.eliminar(cine);
			List<Cine> cines = cineService.listar();
			
			request.setAttribute("cines", cines);
		} catch (DAOExcepcion e) {
			e.printStackTrace();
		}
	
		return new ModelAndView("insertarCine");
	}
	
	
}
